package com.fr.data;

import com.fr.base.Parameter;
import com.fr.general.data.TableDataException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;

public class ParameterLinkedHashSetDataDemo extends AbstractTableData {

    private static final long serialVersionUID = 8818000311745955539L;

    // 字段名枚举
    enum FIELD_NAME {
        GEO_ID, GEO_TYPE_ID, GEO_NAME
    }

    private String[] columNames;

    private LinkedHashSet<LinkedHashMap<String, Object>> rowData;

    public ParameterLinkedHashSetDataDemo() {
        this.parameters = new Parameter[] { new Parameter("jobId"),
                new Parameter("minSalary"), new Parameter("maxSalary") };

        // 填充字段名
        columNames = new String[FIELD_NAME.values().length];
        int i = 0;
        for (FIELD_NAME fieldName : FIELD_NAME.values()) {
            columNames[i] = fieldName.toString();
            i++;
        }

    }

    @Override
    public int getColumnCount() throws TableDataException {
        return columNames.length;
    }

    @Override
    public String getColumnName(int columnIndex) throws TableDataException {
        return columNames[columnIndex];
    }

    @Override
    public int getRowCount() throws TableDataException {
        queryData();
        return rowData.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        queryData();
        int tempRowIndex = 0;
        for (LinkedHashMap<String, Object> row : rowData) {
            if (tempRowIndex == rowIndex) {
                return row.get(columNames[columnIndex]);
            }
            tempRowIndex += 1;
        }
        return null;
    }

    // 查询数据
    public void queryData() {
        // 确保只被执行一次
        if (rowData != null) {
            return;
        }

        // 传入的参数
//        String jobId = parameters[0].getValue().toString();
//        float minSalary = Float.parseFloat(parameters[1].getValue().toString());
//        float maxSalary = Float.parseFloat(parameters[2].getValue().toString());

        // 拼装SQL
        String sql = "SELECT g.GEO_ID,g.GEO_TYPE_ID,g.GEO_NAME FROM geo g";

        rowData = new LinkedHashSet<LinkedHashMap<String, Object>>();

        Connection conn = ConnectUtil.getConnection();
        try {
            Statement stmt = conn.createStatement();
            // 执行查询
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                // 填充行数据
                // 注：字段赋值的顺序，要跟枚举里的顺序一样
                LinkedHashMap<String, Object> row = new LinkedHashMap<String, Object>();
                row.put(FIELD_NAME.GEO_ID.toString(),
                        rs.getString(FIELD_NAME.GEO_ID.toString()));
                row.put(FIELD_NAME.GEO_TYPE_ID.toString(),
                        rs.getString(FIELD_NAME.GEO_TYPE_ID.toString()));
                row.put(FIELD_NAME.GEO_NAME.toString(),
                        rs.getString(FIELD_NAME.GEO_NAME.toString()));
                rowData.add(row);
            }
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    // 释放资源
    @Override
    public void release() throws Exception {
        super.release();
        this.rowData = null;
    }

}